package com.readword.util;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.readword.entity.Examination;
import com.readword.entity.TextOrImg;

public class HtmlUtil {

	private final static Logger logger = LoggerFactory.getLogger(HtmlUtil.class);
	
	StringBuilder sb = new StringBuilder();

	private String titleFont = "style=\"font-size: 18px;line-height: 20px;font-weight: bold;text-align: center;\"";
	
	private String bodyFont = "style=\"font-size: 14px;line-height: 15px;\"";
	
	protected HtmlUtil() {}

	public HtmlUtil(String title, List<Examination> list) {
		if("".equals(title) || null == title) {
			title = "Hello word";
		}
		title(title);
		content(list);
	}
	
	public String outHtml() {
		StringBuilder html = new StringBuilder();
		html.append("<!DOCTYPE html>");
		html.append("<html>");
		html.append("<head>");
		html.append("<title></title>");
		html.append("<style>");
		html.append("body{font-family: SimSun;}");
		html.append("@page{size: A4;margin: 0;}");
		html.append("</style>");
		html.append("</head>");
		html.append("<body>");
		html.append(sb);
		html.append("</body>");
		html.append("</html>");
		String htmlStr = html.toString();
		logger.info("生成结果：",htmlStr);
		return htmlStr;
	}
	
	private StringBuilder title(String t) {
		sb.append("<p " + titleFont + ">");
		sb.append(t);
		sb.append("</p>");
		return sb;
	}
	
	private void content(List<Examination> list) {
		if(null != list && list.size() > 0) {
			for (Examination exam : list) {
				if(null != exam.getItems() && exam.getItems().size() > 0) {
					//存在子项
					topicText(exam.getTopics());
					imgsDom(exam.getImgs());
					for (Examination examItem : exam.getItems()) {
						topicText(examItem.getTopics());
						mainText(examItem);
					}
					sb.append("<br></br>");
				}else {
					topicText(exam.getTopics());
					mainText(exam);
					sb.append("<br></br>");
				}
			}
		}
	}
	
	private void topicText(List<TextOrImg> topics) {
		sb.append("<div " + bodyFont + ">");
		for (TextOrImg info : topics) {
			sb.append("<span>");
			if(info.getType() == 0) {
				sb.append(info.getVal());
			}else {
				sb.append("<img style=\"\" src=\""+ info.getVal() +"\" />");
			}
			sb.append("</span>");
		}
		sb.append("</div>");
	}
	
	private void mainText(Examination exam) {
		imgsDom(exam.getImgs());
		for (int i = 0; i < exam.getOptions().size(); i++) {
			String option = exam.getOptions().get(i);
			pDom(Cons.TOOLS.getABCD().get(i), option);
		}
		pDom(Cons.KEYWORD.ANSWER,     exam.getAnswer());
		pDom(Cons.KEYWORD.ANALYSIS,   exam.getAnalysis());
		pDom(Cons.KEYWORD.EXAMSITE,   exam.getExamSite());
		pDom(Cons.KEYWORD.SOURCE,     exam.getSource());
		pDom(Cons.KEYWORD.ERRORPRONE, exam.getErrorProne());
	}
	
	private void pDom(String key, String val) {
		sb.append("<div " + bodyFont + ">");
		sb.append("<span>");
		sb.append(Cons.KEYWORD.getStr(key) + ":");
		sb.append("</span>");
		sb.append("<span>");
		sb.append(Cons.TOOLS.ifNull(val));
		sb.append("</span>");
		sb.append("</div>");
	}
	
	private void imgsDom(List<String> imgs) {
		if(null != imgs && imgs.size() > 0) {
			sb.append("<div>");
			for (String src : imgs) {
				sb.append("<img style=\"height:150px;\" src=\""+ src +"\" />");
			}
			sb.append("</div>");
		}
	}
}
